﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityModel;
using BusinessLayer;
using System.IO;
using System.Data.Linq;

namespace PL___Web_Application
{
    public partial class ViewTruck : PagesMethods
    {
        private Truck showTruck;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string truck_reg = Request.QueryString["id"];
                BL controller = new BL();
                showTruck = controller.getTruck(truck_reg);
                regnumber_lb.Text = showTruck.Vehicle.reg_number;
                fuel_lb.Text = controller.getFuelName((int)showTruck.Vehicle.fuel);
                price_lb.Text = showTruck.Vehicle.price.ToString();
                colour_lb.Text = showTruck.Vehicle.colour;
                state_car_lb.Text = controller.getStateName((int)showTruck.Vehicle.state);
                num_wheels_lb.Text = showTruck.num_wheels.ToString();
                tonnage_lb.Text = showTruck.tonnage.ToString();
                High_lb.Text = showTruck.high.ToString();
                large_lb.Text = showTruck.large.ToString();
                width_lb.Text = showTruck.width.ToString();
                if (state_car_lb.Text == "Available")
                {
                    buy_button.Visible = true;
                    TextBoxNMonths.Visible = true;
                    num_month_lb.Visible = true;
                }
                car_img.ImageUrl = "Image.aspx?id=" + truck_reg + "&type=truck";
            }
            catch (Exception ex) {
                error_lb.Text = ex.Message;
            }   
        }
        protected void BuyTruck(object sender, EventArgs e)
        {
            try
            {
                if (TextBoxNMonths.Text != ""){
                    int nMonths = Convert.ToInt32(TextBoxNMonths.Text);
                    CLeasing ls = new CLeasing(showTruck, GetBusinessFromCookie(), nMonths, DateTime.Now);
                    showTruck.Vehicle.state = 2;                
                    BL controller = new BL();
                    controller.updateTruck(showTruck);
                    controller.AddLeasingContract(ls);
                    String scriptText = "alert('Your lease has been registered, our staff will contact you to request more data')";
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", scriptText, true);
                    Response.Redirect("BusinessAccount.aspx");
                }else{
                    error_lb.Text = "Please Introduce a number of months to lease";
                }
            }
            catch {
                String scriptText = "alert('Error registering the lease, contact with our staff please')";
                ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", scriptText, true);
            }

           
        }
       
    }
}